{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.243136Z",
     "start_time": "2025-01-07T17:30:53.778495Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "series 是一种一维数据结构，类似于带标签的数组。它的索引操作类似于 Python 的字典。\n",
    "\n",
    "DataFrame 是一种二维数据结构，类似于表格。它的索引操作可以针对行和列。"
   ],
   "id": "361a7d97a60f02d7"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### series的相关操作",
   "id": "51da325c6e156fd1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.252018Z",
     "start_time": "2025-01-07T17:30:54.245960Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过list构建Series\n",
    "ser_obj = pd.Series(range(10, 20))\n",
    "print(ser_obj.head(3))\n",
    "print(ser_obj)\n",
    "print(type(ser_obj))"
   ],
   "id": "34451998cbc000ea",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    10\n",
      "1    11\n",
      "2    12\n",
      "dtype: int64\n",
      "0    10\n",
      "1    11\n",
      "2    12\n",
      "3    13\n",
      "4    14\n",
      "5    15\n",
      "6    16\n",
      "7    17\n",
      "8    18\n",
      "9    19\n",
      "dtype: int64\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.257196Z",
     "start_time": "2025-01-07T17:30:54.252018Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#获取数据\n",
    "print(ser_obj.values)\n",
    " #获取索引\n",
    "print(ser_obj.index)"
   ],
   "id": "fa1e118af7b7536b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 11 12 13 14 15 16 17 18 19]\n",
      "RangeIndex(start=0, stop=10, step=1)\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.262389Z",
     "start_time": "2025-01-07T17:30:54.257196Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#通过索引获取数据\n",
    "print(ser_obj[0])\n",
    "print(ser_obj[8])"
   ],
   "id": "a1cf86e48e6e6c87",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "18\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.269208Z",
     "start_time": "2025-01-07T17:30:54.263395Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 索引与数据的对应关系不被运算结果影响\n",
    "print(ser_obj * 2)\n",
    "print(ser_obj > 15)"
   ],
   "id": "65bbae6fb7734d54",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    20\n",
      "1    22\n",
      "2    24\n",
      "3    26\n",
      "4    28\n",
      "5    30\n",
      "6    32\n",
      "7    34\n",
      "8    36\n",
      "9    38\n",
      "dtype: int64\n",
      "0    False\n",
      "1    False\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "5    False\n",
      "6     True\n",
      "7     True\n",
      "8     True\n",
      "9     True\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.277413Z",
     "start_time": "2025-01-07T17:30:54.269208Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过dict构建Series\n",
    "year_data = {2001: 17.8, 2002: 20.1, 2003: 16.5}\n",
    "ser_obj2 = pd.Series(year_data)\n",
    "print(ser_obj2.head())\n",
    "print(ser_obj2.index)\n",
    "print(ser_obj2[2001])"
   ],
   "id": "222bc321f5763c91",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2001    17.8\n",
      "2002    20.1\n",
      "2003    16.5\n",
      "dtype: float64\n",
      "Index([2001, 2002, 2003], dtype='int64')\n",
      "17.8\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.283869Z",
     "start_time": "2025-01-07T17:30:54.277413Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# name 属性\n",
    "ser_obj2.name = 'temp'\n",
    "ser_obj2.index.name = 'year'\n",
    "print(ser_obj2.head())"
   ],
   "id": "1e555b689ad412ef",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "year\n",
      "2001    17.8\n",
      "2002    20.1\n",
      "2003    16.5\n",
      "Name: temp, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### DataFrame的相关操作",
   "id": "88d03a1ca4aa8e27"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.290179Z",
     "start_time": "2025-01-07T17:30:54.283869Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过ndarray构建DataFrame\n",
    "t = pd.DataFrame(np.arange(12).reshape((3,4)))\n",
    "print(t)"
   ],
   "id": "b64b769dba700f2b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   0  1   2   3\n",
      "0  0  1   2   3\n",
      "1  4  5   6   7\n",
      "2  8  9  10  11\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.294780Z",
     "start_time": "2025-01-07T17:30:54.290179Z"
    }
   },
   "cell_type": "code",
   "source": [
    "array = np.random.randn(5,4)\n",
    "print(array)"
   ],
   "id": "6ac08642a2b8ed20",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1.06952646e+00 -1.16902123e-01 -2.16473945e-03 -4.53713517e-02]\n",
      " [-9.01685358e-01  2.69009352e+00  4.07396159e-01  4.62760029e-01]\n",
      " [ 5.44292401e-01 -2.08975367e-01  2.96303269e-01 -6.87163922e-01]\n",
      " [-4.00493684e-01  1.11480566e+00  3.70409646e-02  1.79178208e+00]\n",
      " [-9.57396024e-02 -2.08113537e-01  8.88938958e-01  3.66153520e-01]]\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.303529Z",
     "start_time": "2025-01-07T17:30:54.294780Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_obj = pd.DataFrame(array)\n",
    "print(df_obj.head())"
   ],
   "id": "1beeadaa4108e5ef",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0 -1.069526 -0.116902 -0.002165 -0.045371\n",
      "1 -0.901685  2.690094  0.407396  0.462760\n",
      "2  0.544292 -0.208975  0.296303 -0.687164\n",
      "3 -0.400494  1.114806  0.037041  1.791782\n",
      "4 -0.095740 -0.208114  0.888939  0.366154\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.314158Z",
     "start_time": "2025-01-07T17:30:54.303529Z"
    }
   },
   "cell_type": "code",
   "source": [
    "dict_data = {'A': 1,\n",
    "'B': pd.Timestamp('20190926'),\n",
    "'C': pd.Series(1,\n",
    "index=list(range(4)),dtype='float32'),\n",
    "'D': np.array([3] *4,dtype='int32'),\n",
    "'E': [\"Python\",\"Java\",\"C++\",\"C\"],\n",
    "'F': 'wangdao'}\n",
    "df_obj2 = pd.DataFrame(dict_data)\n",
    "print(df_obj2)\n",
    "d1={\"name\":[\"xiaoming\",\"xiaogang\"],\"age\":[20,32],\"tel\":\n",
    "[10086,10010]}\n",
    "print(pd.DataFrame(d1))"
   ],
   "id": "98a5e12ece0bed86",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A          B    C  D       E        F\n",
      "0  1 2019-09-26  1.0  3  Python  wangdao\n",
      "1  1 2019-09-26  1.0  3    Java  wangdao\n",
      "2  1 2019-09-26  1.0  3     C++  wangdao\n",
      "3  1 2019-09-26  1.0  3       C  wangdao\n",
      "       name  age    tel\n",
      "0  xiaoming   20  10086\n",
      "1  xiaogang   32  10010\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.320183Z",
     "start_time": "2025-01-07T17:30:54.314158Z"
    }
   },
   "cell_type": "code",
   "source": [
    "d2 =[{\"name\" : \"xiaohong\" ,\"age\" :32,\"tel\" :10010},{ \"name\":\n",
    "\"xiaogang\" ,\"tel\": 10000} ,{\"name\":\"xiaowang\" ,\"age\":22}]\n",
    "print(pd.DataFrame(d2))"
   ],
   "id": "9e3d6cc7452eb4c1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       name   age      tel\n",
      "0  xiaohong  32.0  10010.0\n",
      "1  xiaogang   NaN  10000.0\n",
      "2  xiaowang  22.0      NaN\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "DataFrame 的索引操作",
   "id": "b2b28baa1dd6f9d4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.327925Z",
     "start_time": "2025-01-07T17:30:54.320183Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建 DataFrame\n",
    "data = {\n",
    "    'name': ['Alice', 'Bob', 'Charlie'],\n",
    "    'age': [25, 30, 35],\n",
    "    'city': ['New York', 'Los Angeles', 'Chicago']\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "print(df)"
   ],
   "id": "d02e1f88e0a7d966",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      name  age         city\n",
      "0    Alice   25     New York\n",
      "1      Bob   30  Los Angeles\n",
      "2  Charlie   35      Chicago\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.333587Z",
     "start_time": "2025-01-07T17:30:54.327925Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 访问单列\n",
    "print(df['name'])"
   ],
   "id": "79bce377345c3c02",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0      Alice\n",
      "1        Bob\n",
      "2    Charlie\n",
      "Name: name, dtype: object\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.339931Z",
     "start_time": "2025-01-07T17:30:54.333587Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 访问多列\n",
    "print(df[['name', 'city']])"
   ],
   "id": "65006788cd2da497",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      name         city\n",
      "0    Alice     New York\n",
      "1      Bob  Los Angeles\n",
      "2  Charlie      Chicago\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.344210Z",
     "start_time": "2025-01-07T17:30:54.339931Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过索引访问单行\n",
    "print(df.loc[1])"
   ],
   "id": "47a40053e3e5fd46",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name            Bob\n",
      "age              30\n",
      "city    Los Angeles\n",
      "Name: 1, dtype: object\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.349566Z",
     "start_time": "2025-01-07T17:30:54.344210Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过索引访问多行\n",
    "print(df.loc[[0, 2]])"
   ],
   "id": "1a575666b17bd867",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      name  age      city\n",
      "0    Alice   25  New York\n",
      "2  Charlie   35   Chicago\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:54.355800Z",
     "start_time": "2025-01-07T17:30:54.349566Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 布尔索引\n",
    "print(df[df['age'] > 25])"
   ],
   "id": "8c80a8c651220497",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      name  age         city\n",
      "1      Bob   30  Los Angeles\n",
      "2  Charlie   35      Chicago\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "apply() 方法",
   "id": "3e402f5db48d78f4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:30:56.069214Z",
     "start_time": "2025-01-07T17:30:56.063527Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 对 Series运用apply（）\n",
    "s = pd.Series([1, 2, 3, 4])\n",
    "s_squared = s.apply(lambda x: x ** 2)\n",
    "print(s_squared)"
   ],
   "id": "1dd9a79e0eb64e5c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0     1\n",
      "1     4\n",
      "2     9\n",
      "3    16\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:31:22.089544Z",
     "start_time": "2025-01-07T17:31:22.080705Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 对 DataFrame 的每一列运用apply（）\n",
    "df = pd.DataFrame({\n",
    "    'a': [1, 2, 3],\n",
    "    'b': [4, 5, 6]\n",
    "})\n",
    "df_squared = df.apply(lambda x: x ** 2)\n",
    "print(df_squared)"
   ],
   "id": "1d7ee202b922eece",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   a   b\n",
      "0  1  16\n",
      "1  4  25\n",
      "2  9  36\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:31:39.247861Z",
     "start_time": "2025-01-07T17:31:39.237414Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 对 DataFrame 的每一行运用apply（）\n",
    "df_sum = df.apply(lambda row: row.sum(), axis=1)\n",
    "print(df_sum)"
   ],
   "id": "cc2dade14f0fd46",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    5\n",
      "1    7\n",
      "2    9\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "map()方法",
   "id": "d2c1f8a3e650bd10"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:31:57.418480Z",
     "start_time": "2025-01-07T17:31:57.403481Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 对 Series 应用 map\n",
    "s = pd.Series(['apple', 'banana', 'cherry'])\n",
    "s_upper = s.map(lambda x: x.upper())\n",
    "print(s_upper)"
   ],
   "id": "7f348aaed6be0e34",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0     APPLE\n",
      "1    BANANA\n",
      "2    CHERRY\n",
      "dtype: object\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T17:33:55.195725Z",
     "start_time": "2025-01-07T17:33:55.176578Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 对 DataFrame 的每个元素应用map（）\n",
    "df = pd.DataFrame({\n",
    "    'a': [1, 2, 3],\n",
    "    'b': [4, 5, 6]\n",
    "})\n",
    "df_squared = df.map(lambda x: x ** 2)\n",
    "print(df_squared)\n",
    "#使用applymap（）出现warring"
   ],
   "id": "19115cdd7175b747",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   a   b\n",
      "0  1  16\n",
      "1  4  25\n",
      "2  9  36\n"
     ]
    }
   ],
   "execution_count": 26
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
